home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d3 / rettig.arc / TRSOURCE.EXE / CENTER.C < prev    next >
C/C++ Source or Header  |  1990-10-22  |  2KB  |  64 lines

  1. /*********
  2. * CENTER.C
  3. *
  4. * by Tom Rettig
  5. *
  6. * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
  7. *
  8. *  Syntax: CENTER( <expC>, <expN> )
  9. *  Return: <expC> preceded with <expN>/2 spaces.
  10. *          Unchanged <expC> if <expN> is <= LEN(<expC>).
  11. *  Note  : Does not trim leading or trailing spaces from <expC>.
  12. *********/
  13.  
  14. #include "trlib.h"
  15.  
  16. TRTYPE center()
  17. {
  18.    static char funcname[] = { "center" };
  19.    char *instr, *ret;
  20.    int i, j, spaces, len, size;
  21.  
  22.    if ( PCOUNT==2 && ISCHAR(1) && ISNUM(2) )
  23.    {
  24.       instr  = _parc(1);
  25.       spaces = _parni(2);
  26.       len    = _tr_strlen(instr);
  27.       size   = len + 2 + (spaces/2);
  28.       ret    = _tr_allocmem( (unsigned)size);
  29.  
  30.       if (spaces <= 0)   /* if spaces is a negative number */
  31.       {
  32.          _retc(instr);
  33.          _tr_freemem( ret,(unsigned)size);
  34.          return;
  35.       }
  36.           
  37.       if ( ret )
  38.       {
  39.          if ( spaces>len )
  40.          {
  41.             for ( i=0; i<=(spaces-len)/2; i++ )
  42.                ret[i] = SPACEC;
  43.  
  44.             for ( j=0; instr[j]; i++, j++ )
  45.                ret[i] = instr[j];
  46.  
  47.             ret[i] = NULLC;
  48.  
  49.             _retc( ret );
  50.             _tr_freemem( ret,(unsigned)size);
  51.          }
  52.          else   /* return unchanged if spaces < len of string */
  53.          {
  54.             _retc( instr );
  55.             _tr_freemem( ret,(unsigned)size);
  56.          }
  57.       }
  58.       else
  59.          _retc( _tr_errmsgs(funcname,E_ALLOC) );
  60.    }
  61.    else
  62.       _retc( _tr_errmsgs(funcname,E_SYNTAX) );
  63. }
  64.